package com.jwds.digital.bios.business.service.service.dispatch.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jwds.digital.bios.business.core.exceptions.RecordNotFoundException;
import com.jwds.digital.bios.business.core.infrastructure.dto.dispatch.DispatchRevertReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.dto.dispatch.assembler.DispatchRevertAssembler;
import com.jwds.digital.bios.business.core.infrastructure.dto.feign.BasicStockBinReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.dto.feign.FlowBasicReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.entity.dispatch.DispatchFeed;
import com.jwds.digital.bios.business.core.infrastructure.entity.dispatch.DispatchRevert;
import com.jwds.digital.bios.business.core.infrastructure.mapper.dispatch.DispatchRevertMapper;
import com.jwds.digital.bios.business.core.infrastructure.vo.dispatch.DispatchFeedRespVO;
import com.jwds.digital.bios.business.core.infrastructure.vo.dispatch.DispatchRevertRespVO;
import com.jwds.digital.bios.business.core.infrastructure.vo.feign.BasicStockBinRespVO;
import com.jwds.digital.bios.business.core.infrastructure.vo.feign.FlowBasicRespVO;
import com.jwds.digital.bios.business.core.utils.FeignResultUtil;
import com.jwds.digital.bios.business.rpc.feign.BasicStockBinFeign;
import com.jwds.digital.bios.business.rpc.feign.FlowBasicFeign;
import com.jwds.digital.bios.business.service.service.dispatch.IDispatchRevertService;
import com.jwds.tdc.framework.application.lib.BaseResponse;
import com.jwds.tdc.framework.application.lib.page.PageRequest;
import com.jwds.tdc.framework.application.lib.page.PageResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 返料调度实现类
 *
 * @author 林柏云
 * @date 2024-10-24
 */
@Slf4j
@Service
public class DispatchRevertServiceImpl extends ServiceImpl<DispatchRevertMapper, DispatchRevert> implements IDispatchRevertService {

    @Autowired
    private DispatchRevertMapper baseMapper;
    @Autowired
    private BasicStockBinFeign basicStockBinFeign;
    @Autowired
    private FlowBasicFeign flowBasicFeign;

    @Override
    public DispatchRevertRespVO queryById(Long id) {
        if (id == null) {
            return null;
        }
        DispatchRevert entity = baseMapper.selectById(id);
        return DispatchRevertAssembler.entity2dto(entity);
    }

    @Override
    public PageResponse<DispatchRevertRespVO> page(PageRequest<DispatchRevertReqDTO> request) {
        DispatchRevertReqDTO condition = request.getQueryCondition();
        Page<DispatchRevertRespVO> page = new Page<>(request.getPageIndex(), request.getPageSize());
        IPage<DispatchRevertRespVO> pageData = baseMapper.pageQuery(page, condition);
        return new PageResponse<>((Page<?>) pageData, pageData.getRecords());
    }

    @Override
    public List<DispatchRevertRespVO> findAll() {
        return baseMapper.queryList();
    }

    @Override
    public List<String> DropDownBox(String destPointion) {
        if (destPointion == null){
            throw new RecordNotFoundException();
        }
        ArrayList<String> arrayList = new ArrayList<>();
        LambdaQueryWrapper<DispatchRevert> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DispatchRevert::getDestPointion,destPointion);
        List<DispatchRevert> list = baseMapper.selectList(queryWrapper);
        for (DispatchRevert datum : list) {
            arrayList.add(datum.getDestPointion());
        }
        return arrayList;
    }

    @Override
    public List<String> destDevice(String destDevice) {
        if (destDevice == null){
            throw new RecordNotFoundException();
        }
        ArrayList<String> arrayList = new ArrayList<>();
        LambdaQueryWrapper<DispatchRevert> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DispatchRevert::getDestDevice,destDevice);
        List<DispatchRevert> list = baseMapper.selectList(queryWrapper);
        for (DispatchRevert datum : list) {
            arrayList.add(datum.getDestDevice());
        }
        return arrayList;
    }

    @Override
    public List<String> originDevice(DispatchRevertReqDTO reqDTO) {
        if (reqDTO == null){
            throw new RecordNotFoundException();
        }
        DispatchRevert entity = baseMapper.selectById(reqDTO.getId());
//        通过起点位置的名称获取起点设备的编码
        BasicStockBinReqDTO basicStockBinReqDTO = new BasicStockBinReqDTO();
        basicStockBinReqDTO.setBinName(entity.getOriginPointion());
        List<FlowBasicReqDTO> result =  new ArrayList<>();
        List<BasicStockBinRespVO> binName = FeignResultUtil.getDataFromBaseResponse(basicStockBinFeign.list(basicStockBinReqDTO));
        for (BasicStockBinRespVO datum : binName) {
            FlowBasicReqDTO dto = new FlowBasicReqDTO();
            dto.setStartDeviceCode(datum.getDeviceOut());
            result.add(dto);
        }
//        通过终点位置的名称获取起点的编码
        basicStockBinReqDTO.setBinName(reqDTO.getDestPointion());
        List<BasicStockBinRespVO> flowName = FeignResultUtil.getDataFromBaseResponse(basicStockBinFeign.list(basicStockBinReqDTO));
        for (BasicStockBinRespVO datum : flowName) {
            FlowBasicReqDTO dto = new FlowBasicReqDTO();
            dto.setEndDeviceCode(datum.getDeviceIn());
            result.add(dto);
        }
        BaseResponse<List<FlowBasicRespVO>> all = flowBasicFeign.all((FlowBasicReqDTO) result);
        List<FlowBasicRespVO> data = all.getData();
        List<String> list = new ArrayList<>();
        for (FlowBasicRespVO datum : data){
            list.add(datum.getFlowName());
        }
        return list;
    }

    @Override
    public DispatchRevertRespVO execute(DispatchRevertReqDTO reqDTO) {
        DispatchRevert entity = baseMapper.selectById(reqDTO.getId());
//      通过起点位置的名称获取起点设备的编码
        BasicStockBinReqDTO basicStockBinReqDTO = new BasicStockBinReqDTO();
        basicStockBinReqDTO.setBinName(entity.getOriginPointion());
        List<FlowBasicReqDTO> result =  new ArrayList<>();
        List<BasicStockBinRespVO> originCode = FeignResultUtil.getDataFromBaseResponse(basicStockBinFeign.list(basicStockBinReqDTO));
        for (BasicStockBinRespVO datum : originCode) {
            FlowBasicReqDTO dto = new FlowBasicReqDTO();
            dto.setStartDeviceCode(datum.getDeviceOut());
            result.add(dto);
        }
//        通过终点设备的名称获取起点设备的编码
        basicStockBinReqDTO.setBinName(entity.getDestPointion());
        List<BasicStockBinRespVO> destCode = FeignResultUtil.getDataFromBaseResponse(basicStockBinFeign.list(basicStockBinReqDTO));
        for (BasicStockBinRespVO datum : destCode) {
            FlowBasicReqDTO dto = new FlowBasicReqDTO();
            dto.setEndDeviceCode(datum.getDeviceIn());
            result.add(dto);
        }
        BaseResponse<List<FlowBasicRespVO>> all = flowBasicFeign.all((FlowBasicReqDTO) result);
        List<FlowBasicRespVO> data = all.getData();
        List<String> list = data.stream().map(FlowBasicRespVO::getFlowName).collect(Collectors.toList());
        entity.setProcessRoute(list.toString());
        baseMapper.updateById(entity);
        DispatchRevertRespVO dispatchRevertRespVO = new DispatchRevertRespVO();
        BeanUtils.copyProperties(entity,dispatchRevertRespVO);
        return dispatchRevertRespVO;

    }

    @Override
    public List<DispatchRevertRespVO> listBlastFurnace(String binName) {
        BasicStockBinReqDTO basicStockBinReqDTO = new BasicStockBinReqDTO();
        basicStockBinReqDTO.setBinName(binName);
        //查询指定的料仓
        List<BasicStockBinRespVO> listBaseStockBin = FeignResultUtil.getDataFromBaseResponse(basicStockBinFeign.list(basicStockBinReqDTO));
        //根据料仓编码查询指定的料仓
        LambdaQueryWrapper<DispatchRevert> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DispatchRevert::getBinCode,listBaseStockBin.stream().findFirst().get().getBinCode());
        List<DispatchRevert> dispatchRevertList = baseMapper.selectList(queryWrapper);
        ArrayList<DispatchRevertRespVO> dispatchRevertRespVOList = new ArrayList<>();
        for (DispatchRevert dispatchRevert : dispatchRevertList) {
            DispatchRevertRespVO dispatchRevertRespVO = new DispatchRevertRespVO();
            BeanUtil.copyProperties(dispatchRevert, dispatchRevertRespVO);
            dispatchRevertRespVOList.add(dispatchRevertRespVO);
        }
        return dispatchRevertRespVOList;
    }


}
